package cn.rongdao.jrkabao.ui.remind.treeviewbinder;

import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.remind.RemindBillBean;
import cn.rongdao.jrkabao.treeview.TreeNode;

/**
 * Created by zxy on 17/4/23
 */

public class FirstLevelNodeViewBinder extends CheckableNodeViewBinder {

    private TextView tvDate;
    private ImageView ivArrow;
    private TextView tvMoney;

    FirstLevelNodeViewBinder(View itemView) {
        super(itemView);
        tvDate = (TextView) itemView.findViewById(R.id.irfl_tv_date);
        ivArrow = (ImageView) itemView.findViewById(R.id.arrow_img);
        tvMoney = (TextView) itemView.findViewById(R.id.irfl_tv_money);
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_remind_first_level;
    }

    @Override
    public void bindView(TreeNode treeNode) {
        RemindBillBean remindBillBean= (RemindBillBean) treeNode.getValue();
        tvMoney.setText(FRString.valueOf(remindBillBean.getAmount()));
        tvDate.setText(getTextContent(remindBillBean.getMonth(), remindBillBean.getDate()));
    }

    private SpannableString getTextContent(String billMonth, String billRange) {
        String content = FRCollection.appendAsString(billMonth, "\n", billRange, "");
        int index = content.indexOf("/");
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.c333333)), 0, index, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        ss.setSpan(new AbsoluteSizeSpan(16, true), 0, index, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        ss.setSpan(new AbsoluteSizeSpan(12, true), index, billMonth.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return ss;
    }

    @Override
    public void onNodeToggled(TreeNode treeNode, boolean expand) {
        if (expand) {
            ivArrow.animate().rotation(180).setDuration(200).start();
        } else {
            ivArrow.animate().rotation(0).setDuration(200).start();
        }
    }
}
